<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/include/tag.jsp"%>
<%@ include file="/WEB-INF/include/common.jsp"%>
<link rel="stylesheet" href="${ctx}/statics/css/custom_theme.css" />
<div class="modal fade" id="userRoleModel" tabindex="-1" role="dialog" aria-labelledby="userRoleModelLabel" aria-hidden="true" data-backdrop="false">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                	<span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="userRoleModelLabel">${subTitle}</h4>
            </div>
            <div class="modal-body">
        		<div id="userRoleModel_modal_body"></div>
            </div>
        </div>
         
    </div>
</div>

<!-- 模态窗口 -->
<div class="modal fade" id="userModel" tabindex="-1" role="dialog" aria-labelledby="userModelLabel" aria-hidden="true" data-backdrop="false">
  <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
            	<button type="button" class="close" data-dismiss="modal" aria-label="Close">
                	<span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" >添加活动</h4>
            </div>
            <div class="modal-body">
            	<form id="act_edit_form"  class="form-horizontal">
			        <input type="hidden" id="id" name="id">
			        <div class="form-group">
			        	<label class="control-label col-sm-3">活动类型：</label>
			        	<div class="col-sm-8">
			        		<select id="activities_type" name="activitiesType" class="form-control">
			        		<option value="0">--请选择--</option>
			                  <c:forEach var="activities" items="${activitiesList}" varStatus="status" >
								<option value="${activities.type }">${activities.type }</option>
			                  </c:forEach>
							</select>
						</div>
		        	</div>
		        	<div class="form-group">
		        		<label class="control-label col-sm-3">车贷产品：</label>
		        		<div class="col-sm-8">
			        		<select name="loanId" class="form-control">
			        		<option value="0">--请选择--</option>
			                  <c:forEach var="loan" items="${loanList}" varStatus="status" >
								<option value="${loan.id }">${loan.productName }</option>
			                  </c:forEach>
							</select>
						</div>
					</div>
			        <div class="form-group">
			        	<label class="control-label col-sm-3" >开始时间：</label>
			        	<div class="col-sm-8">
			            	<input type="text" class="form-control" name="beginTime" id="beginTime" required="true"/>
			       		 </div>
			        </div>
			        <div class="form-group">
			        	<label class="control-label col-sm-3" >结束时间：</label>
			        	<div class="col-sm-8">
			            	<input type="text" class="form-control" name="endTime" id="endTime" required="true" />
			       		 </div>
			        </div>
			        <div class="form-group">
			        	<label class="control-label col-sm-3" id="youhui_du">优惠度：</label>
			        	<div class="col-sm-8">
				            <input type="text"  id="activityRate" name="activityRate" class="form-control">
				            <span id="activityRate_error" style="color:red"></span>
			        	</div>
			        </div>
			    </form>
            </div>
            <div class="modal-footer">
            	<button type="button" class="btn btn-sm btn-primary" id="userModelBtn">确定</button>
                <button type="button" class="btn btn-sm btn-default" data-dismiss="modal" id="Close">关闭</button>
            </div>
        </div>
   </div>
</div>
<div id="main-content">
	<div class="row" >
		<div class="col-md-12">
			<div class="panel panel-default">
				<div class="panel-heading">${title}</div>
				<!-- /.panel-heading -->
				<div class="panel-body" >
					<div class="row">
						<div class="col-xs-12">
							<!-- PAGE CONTENT BEGINS -->
							<form class="form-horizontal" role="form" id="user_search_form">
								<div class="col-sm-9">
									<label class="col-sm-2 control-label">活动类型</label>
									<div class="col-sm-3">
										<input id="search_realName" name="activitiesType" type="text"/> 
									</div>
						  
								</div>
								<div class="col-sm-3">
									<div class="col-sm-4">
										<button class="btn btn-sm btn-info" type="submit">
											<i class="icon-search nav-search-icon"></i>查询
										</button> 
									</div>
									<div class="col-sm-4"> 
										<button type="button" class="btn btn-success btn-sm" onclick="addData()"><i class="icon-plus bigger-110"></i>新增</button>
									</div>
									<div class="col-sm-4">
										<button class="btn btn-sm btn-primary" type="reset"	>
										<i class="icon-undo bigger-110"></i>清除
									</button>
									</div>
								</div>
							</form>
						</div>
					</div>
					<table id="user_table" class="table table-bordered table-hover">
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
function youhui_du(){
	var youhui_du = $("#activities_type").val();
	if(youhui_du=="优惠"){
		$("#youhui_du").attr("data-value",0);
		$("#youhui_du").text("优惠金额：")
	}else if( youhui_du=="折扣"){
		$("#youhui_du").attr("data-value",1);
		$("#youhui_du").text("折扣率(%)：")
	}
}
$(function(){
	$("#activities_type").change(function(){
		youhui_du();
	});
	$('#user_table').bootstrapTable({
		url: '${ctx}/admin/activities/queryActLoanList.do',
		pagination: true,
		paginationLoop: false,
		smartDisplay: false,
	    pageSize: 10,
	    pageList: [10, 20, 50, 100],
	    sidePagination: 'server',
	    paginationNextText:'下一页',
	    paginationPreText:'上一页',
	    cache: false,
		columns: [
	   {
		field: '',
		title: '操作',
		align: 'center',
		width :'120px',
		formatter: function(value, row, index){
			return '<button class="btn btn-xs btn-info" id="editData" title="编辑"><i class="icon-edit bigger-130"></i></button>'
					+'<button class="btn btn-xs btn-danger" id="deleteData" title="删除"><i class="icon-trash bigger-130"></i></button>';
		
		},
		events: {
			'click #deleteData':deleteData,
			'click #editData':editData,
		  }
 		},{
			field: 'id',
			title: 'ID',
			align: 'center'
		},{
			field: 'activitiesType',
			title: '活动类型',
			align: 'center'
		},{
			field: 'beginTime',
			formatter:function(value,row,index){
				var date = new Date(value);
				//date.getFullYear();//获取完整的年份(4位,1970)
				//date.getMonth();//获取月份(0-11,0代表1月,用的时候记得加上1)
				//date.getDate();//获取日(1-31)
				
				return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
			},
			title: '开始时间',
			align: 'center'
		},{
			field: 'endTime',
			formatter:function(value,row,index){
				var date = new Date(value);
				//date.getFullYear();//获取完整的年份(4位,1970)
				//date.getMonth();//获取月份(0-11,0代表1月,用的时候记得加上1)
				//date.getDate();//获取日(1-31)
				
				return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
			},
			title: '结束时间',
			align: 'center'
		},{
			field: 'loan.productName',
			title: '车贷产品名称',
			align: 'center'
		},{
			field: 'activityRate',
			title: '优惠度',
			align: 'center',
			formatter:function(value,row,index){
				var rateType = row.activitiesType;
				if(rateType=="折扣"){
					value += "%";
				}else{
					value+="元";
				}
				return value;
			},
		}],
		queryParams: function(params){
			//console.log(params);
			//params.usreId
			$.extend(params,$("#user_search_form").serializeJson());
			return params;
		}
	});

	$('#user_table').on('mouseenter','.pannel_a_title',function () {
        $(this).popover('show');
	});
	
	$.validator.addMethod("unequal",function(value,element,param){  
		return this.optional( element ) || value != param;
	},"请选择");
	//添加和编辑请求验证
	$("#act_edit_form").validate({
		rules: {
			//loginName:{required:true,maxlength:30},
			activityRate:{required:true,number : true},
			activitiesType : {unequal : 0},
			loanId : {unequal : 0},
		},
		messages : {
			activityRate:{required:"必填",number:"只能填数字"},
			//realName:{required:"必填",maxlength:"最大长度30"},
		}, 
		submitHandler: function(form) {
			var date1=$("#beginTime").val();
			var date2=$("#endTime").val();
			if(date2<date1){
				alert("结束时间不能在开始时间之前");
				return;
			}
			var activitiesType = $("#activities_type").val();
			var rate = $("#activityRate").val();
			if(activitiesType=="折扣"){
				if(rate<=0 || rate>100){
					$("#activityRate_error").text("请输入0到100之间的数字")
					return;
				}else{
					$("#activityRate_error").text("")
				}
			}
			$("#userModel").prop("disabled",true);
    		$(form).ajaxSubmit({
    			type:"post",
    			dataType:"json",
    			url:"${ctx}/admin/activities/saveOrUpdateActivitiesLoan.do",
    			success:function(data){
    				if(data.success==true){
    					$('#userModel').modal('hide') 
    					$('#user_table').bootstrapTable('refresh');
    				}else{
    					alert(data.errorMessage);
    				}
    				$("#userModel").prop("disabled",false);
                }  
            });
		}
	});
	
	//提交用户信息
	$("#userModel").on('click','#userModelBtn',function(){
		$("#act_edit_form").submit();
	});
	
	//搜索角色
	$("#user_search_form").submit(function(e){
		e.preventDefault();
		$('#user_table').bootstrapTable('refresh');
	});
	
});

//添加
function addData(e, v, row) {
	$("#activityRate_error").text("");
	$("#userModel .modal-title").empty().html("添加车贷活动类型");
	$("#act_edit_form").validate().resetForm();
	$("#act_edit_form input[name=id]").val("");
	$("#act_edit_form select[name=activitiesType]").val("0");
	$("#act_edit_form select[name=loanId]").val("0");
	$("#act_edit_form input[name=beginTime]").val("");
	$("#act_edit_form input[name=endTime]").val("");
	$("#act_edit_form input[name=activityRate]").val("");
	$('#userModel').modal('show');
}

//编辑
function editData(e, v, row) {
	$("#activityRate_error").text("");
	$("#userModel .modal-title").empty().html("修改车贷活动类型");
	$("#act_edit_form").validate().resetForm();
	$("#act_edit_form input[name=id]").val(row.id);
	$("#act_edit_form select[name=activitiesType]").val(row.activitiesType);
	$("#act_edit_form select[name=loanId]").val(row.loan.id);
	var date1 = new Date(row.beginTime);
	var date2 = new Date(row.endTime);

	$("#beginTime").datetimepicker('update',date1);
	$("#endTime").datetimepicker('update',date2);
	$("#act_edit_form input[name=activityRate]").val(row.activityRate);
	youhui_du();
	$('#userModel').modal('show');
}

//删除用户
function deleteData(e, v, row) {
	if(row.dictItemList!=""){
		if(confirm("确定删除吗？")){
			$.ajax({
		        url: "${ctx}/admin/activities/deleteActivitiesLoan.do",
		        type: "POST",
		        data: {
		            "id": row.id
		        }, 
		        success: function (data) {
		        	if(data.success){
		        		$('#user_table').bootstrapTable('refresh');
		            }else {
		            	alert(data.errorMessage);
		            }
		        }
	  		});
		}
	}
	
}

$("#beginTime").datetimepicker({
    minView: "month",
    language:  'zh-CN',
    format: 'yyyy-mm-dd',
    todayHighlight: true,
    todayBtn: true,
    autoclose: true,
   // endDate : new Date() 禁止今天之后的日期
});
$("#endTime").datetimepicker({
    minView: "month",
    language:  'zh-CN',
    format: 'yyyy-mm-dd',
    todayHighlight: true,
    todayBtn: true,
    autoclose: true,
});

</script>